package org.csu.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.equipment.entity.*;
import org.csu.equipment.persistence.*;
import org.csu.equipment.service.AdminService;
import org.csu.equipment.vo.Index2VO;
import org.csu.equipment.vo.UsingQuantity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminLoginMapper adminLoginMapper;
    @Autowired
    ClientMapper clientMapper;
    @Autowired
    ClientLoginMapper clientLoginMapper;
    @Autowired
    EmployeeSkillMapper employeeSkillMapper;
    @Autowired
    EmployeeLoginMapper employeeLoginMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    WarehouseMapper warehouseMapper;
    @Autowired
    AdminLogMapper adminLogMapper;
    @Autowired
    RecordMapper recordMapper;

    @Override
    public Integer login(String AId,String APwd) {
        QueryWrapper<AdminLogin> queryWrapper = new QueryWrapper<AdminLogin>();
        queryWrapper.eq("A_id",AId).eq("A_pwd",APwd);
        Integer count = adminLoginMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public List<Client> getClientList() {
        return clientMapper.selectList(null);
    }

    @Override
    public List<Employee> getEmployeeList() {
        QueryWrapper<Employee> queryWrapper= new QueryWrapper<Employee>();
        queryWrapper.orderByDesc("E_credit");
        return employeeMapper.selectList(queryWrapper);
    }

    @Override
    public List<Task> getTaskList() {
        return taskMapper.selectList(null);
    }

    @Override
    public List<Warehouse> getWareHouseList() {
        return warehouseMapper.selectList(null);
    }

    @Override
    public int Tasknumber(String EId) {
        QueryWrapper<Task> queryWrapper=new QueryWrapper<Task>();
        queryWrapper.eq("E_id",EId);
        int n=taskMapper.selectCount(queryWrapper);
        return n;
    }



    @Override
    public void log(String AId, String log) {
        AdminLog adminLog=new AdminLog();
        Integer LId = adminLogMapper.selectCount(null)+1000;
        adminLog.setAId(AId);
        adminLog.setLogTime(new Timestamp(System.currentTimeMillis()));
        adminLog.setLog(log);
        adminLog.setLogId(LId.toString());
        adminLogMapper.insert(adminLog);
    }

    @Override
    public void in(String type, int quantity) {
        Warehouse warehouse=warehouseMapper.selectById(type);
        if(warehouse!=null){
            warehouse.setQuantity(warehouse.getQuantity()+quantity);
            warehouseMapper.updateById(warehouse);
        }
        else {
            Warehouse warehouse1 = new Warehouse();
            warehouse1.setType(type);
            warehouse1.setQuantity(quantity);
            warehouseMapper.insert(warehouse1);
        }

    }

    @Override
    public void updateCinfo(Client client) {
        clientMapper.updateById(client);
    }

    @Override
    public void updateEinfo(Employee employee) {
          employeeMapper.updateById(employee);
    }

    @Override
    public void deleteCinfo(String CId) {
        clientMapper.deleteById(CId);
    }

    @Override
    public void deleteEinfo(String EId) {
         employeeMapper.deleteById(EId);
    }

    @Override
    public void insertCinfo(Client client,ClientLogin clientLogin) {
        clientMapper.insert(client);
        clientLoginMapper.insert(clientLogin);
    }

    @Override
    public void insertEinfo(Employee employee, EmployeeLogin employeeLogin, EmployeeSkill employeeSkill) {
        employeeMapper.insert(employee);
        employeeSkillMapper.insert(employeeSkill);
        employeeLoginMapper.insert(employeeLogin);
    }

    @Override
    public List<Record> requestedtask() {
        QueryWrapper<Record> queryWrapper=new QueryWrapper<Record>();
        queryWrapper.eq("is_pass","0");
        List<Record> recordList = recordMapper.selectList(queryWrapper);
        return  recordList;
    }

    @Override
    public List<UsingQuantity> getUsingQuantity() {
        List<UsingQuantity> usingQuantityList = new ArrayList<UsingQuantity>();
        Timestamp time11 = Timestamp.valueOf("2021-06-28 00:00:00");
        Timestamp time12 = Timestamp.valueOf("2021-07-04 23:59:59");
        Timestamp time21 = Timestamp.valueOf("2021-07-05 00:00:00");
        Timestamp time22 = Timestamp.valueOf("2021-07-11 23:59:59");
        Timestamp time31 = Timestamp.valueOf("2021-07-12 00:00:00");
        Timestamp time32 = Timestamp.valueOf("2021-07-18 23:59:59");
        Timestamp time41 = Timestamp.valueOf("2021-07-19 00:00:00");
        Timestamp time42 = Timestamp.valueOf("2021-07-25 23:59:59");

        List<Warehouse> warehouseList  = warehouseMapper.selectList(null);

        for (Warehouse ware:warehouseList) {
            UsingQuantity u = new UsingQuantity();
            u.setType(ware.getType());
            usingQuantityList.add(u);
        }
//        UsingQuantity u1 = new UsingQuantity();
//        UsingQuantity u2 = new UsingQuantity();
//        UsingQuantity u3 = new UsingQuantity();
//        UsingQuantity u4 = new UsingQuantity();
//        u1.setType("air");
//        u2.setType("computer");
//        u3.setType("light");
//        u4.setType("printer");
        QueryWrapper<Task> queryWrapper1=new QueryWrapper<Task>();
        queryWrapper1.isNotNull("finish_time").isNotNull("material").between("finish_time",time11,time12);
        List<Task> taskList1 = taskMapper.selectList(queryWrapper1);
        for (Task task:taskList1) {
            String type = task.getMaterial();
            String[] types=type.split(",");
            for (int i=0;i<types.length;i++){
                String [] t=types[i].split(":");
                for (UsingQuantity u:usingQuantityList) {
                    if(t[0].equals(u.getType())){
                        u.setWeek1(u.getWeek1()+Integer.parseInt(t[1]));
                    }
                }
            }
        }
        QueryWrapper<Task> queryWrapper2=new QueryWrapper<Task>();
        queryWrapper2.isNotNull("finish_time").isNotNull("material").between("finish_time",time21,time22);
        List<Task> taskList2 = taskMapper.selectList(queryWrapper2);
        for (Task task:taskList2) {
            String type = task.getMaterial();
            String[] types=type.split(",");
            for (int i=0;i<types.length;i++){
                String [] t=types[i].split(":");
                for (UsingQuantity u:usingQuantityList) {
                    if(t[0].equals(u.getType())){
                        u.setWeek2(u.getWeek2()+Integer.parseInt(t[1]));
                    }
                }
            }
        }
        QueryWrapper<Task> queryWrapper3=new QueryWrapper<Task>();
        queryWrapper3.isNotNull("finish_time").isNotNull("material").between("finish_time",time31,time32);
        List<Task> taskList3 = taskMapper.selectList(queryWrapper3);
        for (Task task:taskList3) {
            String type = task.getMaterial();
            String[] types=type.split(",");
            for (int i=0;i<types.length;i++){
                String [] t=types[i].split(":");
                for (UsingQuantity u:usingQuantityList) {
                    if(t[0].equals(u.getType())){
                        u.setWeek3(u.getWeek3()+Integer.parseInt(t[1]));
                    }
                }
            }
        }
        QueryWrapper<Task> queryWrapper4=new QueryWrapper<Task>();
        queryWrapper4.isNotNull("finish_time").isNotNull("material").between("finish_time",time41,time42);
        List<Task> taskList4 = taskMapper.selectList(queryWrapper4);
        for (Task task:taskList4) {
            String type = task.getMaterial();
            String[] types = type.split(",");
            for (int i = 0; i < types.length; i++) {
                String[] t = types[i].split(":");
                for (UsingQuantity u:usingQuantityList) {
                    if(t[0].equals(u.getType())){
                        u.setWeek4(u.getWeek4()+Integer.parseInt(t[1]));
                    }
                }
            }
        }
        return usingQuantityList;
    }

    @Override
    public boolean updateRecordAndTask(String RId) {
        Record record = recordMapper.selectById(RId);
        Warehouse warehouse = warehouseMapper.selectById(record.getType());
        if(record.getQuantity()>warehouse.getQuantity()){
            return false;
        }
        warehouse.setQuantity(warehouse.getQuantity()-record.getQuantity());
        record.setIsPass(true);
        warehouseMapper.updateById(warehouse);
        recordMapper.updateById(record);
        QueryWrapper<Record> queryWrapper = new QueryWrapper<Record>();
        queryWrapper.eq("is_pass",false).eq("T_id",record.getTId());
        if(recordMapper.selectCount(queryWrapper)==0){
            Task task = taskMapper.selectById(record.getTId());
            task.setTStatus("已申请");
            taskMapper.updateById(task);
        }
        return true;
    }

    @Override
    public void cancelTask(String TId) {
        Task task = taskMapper.selectById(TId);
        Employee employee = employeeMapper.selectById(task.getEId());
        employee.setEStatus(true);
        employeeMapper.updateById(employee);
        task.setTStatus("已取消");
        taskMapper.updateById(task);
        QueryWrapper<Record> queryWrapper = new QueryWrapper<Record>();
        queryWrapper.eq("T_id",TId);
        Record record = new Record();
        record.setIsPass(true);
        recordMapper.update(record,queryWrapper);
    }

    @Override
    public List<Employee> employeeRank() {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<Employee>();
        queryWrapper.orderByDesc("E_credit").last("limit 3");
        List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
        return employeeList;
    }

    @Override
    public Index2VO getIndex2VO() {
        Index2VO index2VO = new Index2VO();
        int[] a = new int[10];
        int[] b = new int[10];

        Timestamp time11 = Timestamp.valueOf("2021-01-01 00:00:00");
        Timestamp time12 = Timestamp.valueOf("2021-01-31 23:59:59");
        Timestamp time21 = Timestamp.valueOf("2021-02-01 00:00:00");
        Timestamp time22 = Timestamp.valueOf("2021-02-28 23:59:59");
        Timestamp time31 = Timestamp.valueOf("2021-03-01 00:00:00");
        Timestamp time32 = Timestamp.valueOf("2021-03-31 23:59:59");
        Timestamp time41 = Timestamp.valueOf("2021-04-01 00:00:00");
        Timestamp time42 = Timestamp.valueOf("2021-04-30 23:59:59");
        Timestamp time51 = Timestamp.valueOf("2021-05-01 00:00:00");
        Timestamp time52 = Timestamp.valueOf("2021-05-31 23:59:59");
        Timestamp time61 = Timestamp.valueOf("2021-06-01 00:00:00");
        Timestamp time62 = Timestamp.valueOf("2021-06-30 23:59:59");
        Timestamp time71 = Timestamp.valueOf("2021-07-01 00:00:00");
        Timestamp time72 = Timestamp.valueOf("2021-07-31 23:59:59");

        QueryWrapper<Task> queryWrapper1=new QueryWrapper<Task>();
        queryWrapper1.isNotNull("finish_time").between("finish_time",time11,time12).in("skill", Arrays.asList("computer","printer","air","light"));
        a[0] = taskMapper.selectCount(queryWrapper1);

        QueryWrapper<Task> queryWrapper2=new QueryWrapper<Task>();
        queryWrapper2.isNotNull("finish_time").between("finish_time",time21,time22).in("skill", Arrays.asList("computer","printer","air","light"));
        a[1] = taskMapper.selectCount(queryWrapper2);

        QueryWrapper<Task> queryWrapper3=new QueryWrapper<Task>();
        queryWrapper3.isNotNull("finish_time").between("finish_time",time31,time32).in("skill", Arrays.asList("computer","printer","air","light"));
        a[2] = taskMapper.selectCount(queryWrapper3);

        QueryWrapper<Task> queryWrapper4=new QueryWrapper<Task>();
        queryWrapper4.isNotNull("finish_time").between("finish_time",time41,time42).in("skill", Arrays.asList("computer","printer","air","light"));
        a[3] = taskMapper.selectCount(queryWrapper4);

        QueryWrapper<Task> queryWrapper5=new QueryWrapper<Task>();
        queryWrapper5.isNotNull("finish_time").between("finish_time",time51,time52).in("skill", Arrays.asList("computer","printer","air","light"));
        a[4] = taskMapper.selectCount(queryWrapper5);

        QueryWrapper<Task> queryWrapper6=new QueryWrapper<Task>();
        queryWrapper6.isNotNull("finish_time").between("finish_time",time11,time12).notIn("skill", Arrays.asList("computer","printer","air","light"));
        b[0] = taskMapper.selectCount(queryWrapper6);

        QueryWrapper<Task> queryWrapper7=new QueryWrapper<Task>();
        queryWrapper7.isNotNull("finish_time").between("finish_time",time21,time22).notIn("skill", Arrays.asList("computer","printer","air","light"));
        b[1] = taskMapper.selectCount(queryWrapper7);

        QueryWrapper<Task> queryWrapper8=new QueryWrapper<Task>();
        queryWrapper8.isNotNull("finish_time").between("finish_time",time31,time32).notIn("skill", Arrays.asList("computer","printer","air","light"));
        b[2] = taskMapper.selectCount(queryWrapper8);

        QueryWrapper<Task> queryWrapper9=new QueryWrapper<Task>();
        queryWrapper9.isNotNull("finish_time").between("finish_time",time41,time42).notIn("skill", Arrays.asList("computer","printer","air","light"));
        b[3] = taskMapper.selectCount(queryWrapper9);

        QueryWrapper<Task> queryWrapper10=new QueryWrapper<Task>();
        queryWrapper10.isNotNull("finish_time").between("finish_time",time51,time52).notIn("skill", Arrays.asList("computer","printer","air","light"));
        b[4] = taskMapper.selectCount(queryWrapper10);

        index2VO.setAlready(a);
        index2VO.setOther(b);

        QueryWrapper<Task> queryWrapperTC=new QueryWrapper<Task>();
        queryWrapperTC.isNotNull("finish_time").between("finish_time",time71,time72).eq("skill","computer");
        index2VO.setNumOfComputer(taskMapper.selectCount(queryWrapperTC));

        QueryWrapper<Task> queryWrapperTP=new QueryWrapper<Task>();
        queryWrapperTP.isNotNull("finish_time").between("finish_time",time71,time72).eq("skill","printer");
        index2VO.setNumOfPrinter(taskMapper.selectCount(queryWrapperTP));

        QueryWrapper<Task> queryWrapperTL=new QueryWrapper<Task>();
        queryWrapperTL.isNotNull("finish_time").between("finish_time",time71,time72).eq("skill","light");
        index2VO.setNumOfLight(taskMapper.selectCount(queryWrapperTL));

        QueryWrapper<Task> queryWrapperTA=new QueryWrapper<Task>();
        queryWrapperTA.isNotNull("finish_time").between("finish_time",time71,time72).eq("skill","air");
        index2VO.setNumOfAir(taskMapper.selectCount(queryWrapperTA));

        QueryWrapper<Task> queryWrapperTO=new QueryWrapper<Task>();
        queryWrapperTO.isNotNull("finish_time").between("finish_time",time71,time72).notIn("skill", Arrays.asList("computer","printer","air","light"));
        index2VO.setNumOfOther(taskMapper.selectCount(queryWrapperTO));

        QueryWrapper<Task> queryWrapperLO=new QueryWrapper<Task>();
        queryWrapperLO.isNotNull("finish_time").between("finish_time",time61,time62).notIn("skill", Arrays.asList("computer","printer","air","light"));
        index2VO.setLastNumOfLight(taskMapper.selectCount(queryWrapperLO));

        QueryWrapper<Task> queryWrapperLC=new QueryWrapper<Task>();
        queryWrapperLC.isNotNull("finish_time").between("finish_time",time61,time62).eq("skill","computer");
        index2VO.setLastNumOfComputer(taskMapper.selectCount(queryWrapperLC));

        QueryWrapper<Task> queryWrapperLP=new QueryWrapper<Task>();
        queryWrapperLP.isNotNull("finish_time").between("finish_time",time61,time62).eq("skill","printer");
        index2VO.setLastNumOfPrinter(taskMapper.selectCount(queryWrapperLP));

        QueryWrapper<Task> queryWrapperLA=new QueryWrapper<Task>();
        queryWrapperLA.isNotNull("finish_time").between("finish_time",time61,time62).eq("skill","air");
        index2VO.setLastNumOfAir(taskMapper.selectCount(queryWrapperLA));

        QueryWrapper<Task> queryWrapperLL=new QueryWrapper<Task>();
        queryWrapperLL.isNotNull("finish_time").between("finish_time",time61,time62).eq("skill","light");
        index2VO.setLastNumOfLight(taskMapper.selectCount(queryWrapperLL));

        return index2VO;
    }

    @Override
    public List<AdminLog> getLogInTime(String AId, Timestamp startTime,Timestamp endTime) {
        QueryWrapper<AdminLog> queryWrapper = new QueryWrapper<AdminLog>();
        queryWrapper.eq("A_id",AId).between("log_time",startTime,endTime);
        List<AdminLog> adminLogs = adminLogMapper.selectList(queryWrapper);
        return adminLogs;
    }

    @Override
    public Client getAClient(String CId) {
        return clientMapper.selectById(CId);
    }

    @Override
    public Employee getAEmployee(String EId) {
        return employeeMapper.selectById(EId);
    }
}
